/**
 * 
 */
package com.wgo.precise.client.ui.view.util.validators;

import java.net.MalformedURLException;
import java.net.URL;

import org.eclipse.jface.dialogs.IInputValidator;

/**
 * @author petterei
 *
 * @version $Id: UrlInputValidator.java,v 1.1 2006-03-07 14:56:23 petterei Exp $
 */
public class UrlInputValidator implements IInputValidator {

	private String metaErrorMessage = "Invalid URL format:";
	
	
	public UrlInputValidator() {}

	/**
	 * @param metaErrorMessage
	 */
	public UrlInputValidator(String metaErrorMessage) {
		this.metaErrorMessage = metaErrorMessage;
	}

	/**
	 * @see org.eclipse.jface.dialogs.IInputValidator#isValid(java.lang.String)
	 */
	public String isValid(String newText) {
		String errorMessage = null;
		if (null != newText) {		
			try {
				new URL(newText);
			} catch (MalformedURLException e) {
				errorMessage = metaErrorMessage + " " + e.getMessage();
			}
		}
		return errorMessage;
	}

	public String isValid(Object newValue) {
		String errorMessage = null ;
		if (newValue instanceof String) {
			errorMessage = isValid((String)newValue);
		} else if (! (newValue instanceof URL) && null != newValue) {
			errorMessage = metaErrorMessage + " Expected URL or String, was " + newValue.getClass();
		}
		return errorMessage;
	}
}
